#--coding:utf8--

import pymongo
import json

#调试模块
import pdb

from flask import Flask,render_template,request
from werkzeug.contrib.fixers import ProxyFix
from flask.ext.bootstrap import Bootstrap
from flask.ext.moment import Moment
from pymongo import MongoClient
from datetime import datetime

app = Flask(__name__)
bootstrap = Bootstrap(app)
moment = Moment(app)

db = pymongo.MongoClient('localhost',27017).test

@app.route('/')
def index():
    xx = "xiaocan"
    res = cup(xx)
    return render_template('index.html',users=db.user.find(),name=res)

@app.route('/time', methods=['GET', 'POST'])
def time():
    if request.method == 'POST':
        data = request.form.get('tag')
        return data
    # return render_template('time.html',current_time=datetime.utcnow())

def cup(name):
    return "dsakf"+name


@app.route('/json')
def dsaf():
    abc = '{"isOK": 1, "isRunning": "你好", "isError": "adfsa"}'

    #调试语句
    # print json.loads(abc)
    # print type(json.loads(abc))
    # pdb.set_trace()
    return render_template('dasf.html',name=json.loads(abc))

@app.route('/user/<name>')
def user(name):
    return render_template('user/test.html',name=name)

@app.route('/test', methods=['GET', 'POST'])
def test():
    if request.method == 'GET':
        return request.args.get("xxx")
    if request.method == 'POST':
        json_str = json.dumps(request.get_json(force=True))

        # xx = json.loads(json_str)
        #json_str = '{"isOK": 1, "name": "你好", "isError": "adfsa"}'
        xxx = json.loads(json_str)

        #return render_template('test.html',name=xxx["name"])

        return xxx['city']

app.wsgi_app = ProxyFix(app.wsgi_app)
if __name__ == '__main__':
    app.debug = True
    app.run()